import numpy as np
import pandas as pd
from dataclasses import dataclass
from src.common.descriptors.load_data import (
    load_teacher_data, 
    load_stu_data, 
    load_kaoqin_data, 
    load_grade_data, 
    load_consumption_data
    )

@dataclass
class StuDescribe:

    stu_id:str
    
    def filter_consumption_data(self):
        df = load_consumption_data()
        df = df[df['bf_StudentID'].astype("str") == self.stu_id]
        df['dt'] = pd.to_datetime(df['DealTime'])
        df['ymd'] = df['dt'].dt.strftime('%Y-%m-%d')
        df['ym'] = df['dt'].dt.strftime('%Y-%m')
        return df
    
    def filter_user_data(self):
        user_data = load_stu_data()
        stu_info = user_data[user_data['bf_StudentID'].astype("str") == self.stu_id]
        return stu_info
    
    def filter_grade_data(self):
        source_data = load_grade_data()
        return source_data[source_data['mes_StudentID'] == int(self.stu_id)]

    def filter_average_grade_data(self):
        user_data = load_grade_data()
        stu_info = user_data[user_data['mes_StudentID'].astype("str") == self.stu_id]
        group_by_data = stu_info.groupby(by=["mes_sub_name"], as_index=False)['mes_Score'].mean()
        return group_by_data.sort_values(by=['mes_Score'], ascending=False)
    
    def filter_consumption_year_month_data(self):
        user_data = self.filter_consumption_data()
        return user_data.groupby(by=['ym'],as_index=False)['MonDeal'].mean()
    
    def predict_consumption(self):
        output = []
        consump_data = self.filter_consumption_data()
        consump_data.sort_values(by = 'dt',ascending=True,inplace=True)
        last_consumption = consump_data.iloc[-3:,:]
        for i,val in last_consumption.iterrows():
            output.append([val['DealTime'],-val['MonDeal']])
        _predict_values = -float(np.mean(last_consumption['MonDeal'].values))
        output.append([f"下一次消费(预测)",_predict_values])
        return pd.DataFrame(output,columns=['日期','消费金额（元）'])
    
    def filter_attendance_data(self):
        attendance_data = load_kaoqin_data()
        return attendance_data[attendance_data['bf_StudentID'].astype("str") == self.stu_id]
    
    def filter_teacher_data(self):
        user_data = load_stu_data()
        stu_info = user_data[user_data['bf_StudentID'].astype("str") == self.stu_id]
        cla_id,cla_term = str(stu_info['cla_id'].iloc[0]),str(stu_info['cla_term'].iloc[0])
        teacher_data = load_teacher_data()
        teacher_info = teacher_data[(teacher_data['cla_id'].astype("str") == cla_id) & (teacher_data['term'].astype("str") == cla_term)]
        return teacher_info
